package io.renren.sf.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.sf.entity.CbReadingEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbObserveEntity;
import io.renren.sf.service.CbObserveService;
import io.renren.common.utils.R;



/**
 *
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-03-31 13:04:11
 */
@RestController
@RequestMapping("sf/cbobserve")
public class CbObserveController {
    @Autowired
    private CbObserveService cbObserveService;

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbobserve:info")
    public R info(@PathVariable("id") Long id){
		CbObserveEntity cbObserve = cbObserveService.getById(id);

        return R.ok().put("cbObserve", cbObserve);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbobserve:save")
    public R save(@RequestBody CbObserveEntity cbObserve){
        try {
            cbObserveService.saveOrUpdate(cbObserve);
            return R.ok();
        } catch (Exception e) {
            return R.error("该字体已存在观察数据，请刷新页面");
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbobserve:update")
    public R update(@RequestBody CbObserveEntity cbObserve){
		cbObserveService.updateById(cbObserve);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbobserve:delete")
    public R delete(@RequestBody Long[] ids){
		cbObserveService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 通过字体id获取认读以及认读图片
     */
    @GetMapping("/getObserveByWordId/{wordId}")
    public R getObserveByWordId(@PathVariable("wordId") Long wordId){
        CbObserveEntity cbObserveEntity = cbObserveService.getObserveByWordId(wordId);
        return R.ok().put("cbObserve", cbObserveEntity);
    }

}
